﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Mindfor.Web
{
	/// <summary>
	/// Represents readonle module collection.
	/// </summary>
	public class ModuleCollection : ReadOnlyCollection<ModuleBase>
	{
		/// <summary>
		/// Initializes new instance.
		/// </summary>
		/// <param name="modules">Module items.</param>
		public ModuleCollection(IList<ModuleBase> modules)
			: base(modules)
		{
		}

		/// <summary>
		/// Gets module by name. If not found returns null.
		/// </summary>
		/// <param name="name">Module name to search.</param>
		public ModuleBase this[string name]
		{
			get
			{
				foreach (ModuleBase m in Items)
				{
					if (m.Name == name)
						return m;
				}
				return null;
			}
		}

		/// <summary>
		/// Returns enabled modules.
		/// </summary>
		public IEnumerable<ModuleBase> GetEnabledModules()
		{
			foreach (ModuleBase module in this)
			{
				if (module.IsEnabled)
					yield return module;
			}
		}

		/// <summary>
		/// Returns registered modules.
		/// </summary>
		public IEnumerable<ModuleBase> GetRegisteredModules()
		{
			foreach (ModuleBase module in this)
			{
				if (module.State == ModuleState.Registered)
					yield return module;
			}
		}
	}
}
